package com.jtech.together.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jtech.together.R;
import com.yuyh.library.imgsel.ImageLoader;
import com.yuyh.library.imgsel.ImgSelActivity;
import com.yuyh.library.imgsel.ImgSelConfig;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * 图片选择的工具方法
 * Created by wuxubaiyang on 2017/3/23.
 */

public class ImageSelect {
    public static final int REQUEST_IMAGE = 0x9527;

    /**
     * 图片选择，单选
     *
     * @param activity
     * @param needCrop
     */
    public static void singleSelect(Activity activity, boolean needCrop) {
        ImgSelActivity.startActivity(activity, getImageConfig(activity, false, true, false, needCrop, 1), REQUEST_IMAGE);
    }

    /**
     * 图片选择,多选
     *
     * @param activity
     * @param rememberSelect
     */
    public static void multiSelect(Activity activity, boolean rememberSelect, int maxCount) {
        ImgSelActivity.startActivity(activity, getImageConfig(activity, true, true, rememberSelect, false, maxCount), REQUEST_IMAGE);
    }

    /**
     * 图片选择,单选
     *
     * @param fragment
     * @param needCrop
     */
    public static void singleSelect(Fragment fragment, boolean needCrop) {
        ImgSelActivity.startActivity(fragment, getImageConfig(fragment.getContext(), false, true, false, needCrop, 1), REQUEST_IMAGE);
    }

    /**
     * 图片选择，多选
     *
     * @param fragment
     * @param rememberSelect
     */
    public static void multiSelect(Fragment fragment, boolean rememberSelect, int maxCount) {
        ImgSelActivity.startActivity(fragment, getImageConfig(fragment.getContext(), true, true, rememberSelect, false, maxCount), REQUEST_IMAGE);
    }

    /**
     * 判断是否为图片请求的返回
     *
     * @param requestCode
     * @param resultCode
     * @return
     */
    public static boolean isRequestImageResult(int requestCode, int resultCode) {
        return requestCode == REQUEST_IMAGE && resultCode == RESULT_OK;
    }

    /**
     * 判断是否为图片请求的返回
     *
     * @param requestCode
     * @return
     */
    public static boolean isRequestImageResult(int requestCode) {
        return requestCode == REQUEST_IMAGE;
    }

    /**
     * 获取请求的图片
     *
     * @param data
     * @return
     */
    public static List<String> getRequestImage(@NonNull Intent data) {
        if (null == data) {
            return new ArrayList<>();
        }
        return data.getStringArrayListExtra(ImgSelActivity.INTENT_RESULT);
    }

    /**
     * 获取配置
     *
     * @param context
     * @param multiSelect
     * @param needCrop
     * @return
     */
    private static ImgSelConfig getImageConfig(Context context, boolean multiSelect, boolean needCamera,
                                               boolean rememberSelected, boolean needCrop, int maxCount) {
        ImgSelConfig config = new ImgSelConfig.Builder(context, new OnImageLoader())
                .multiSelect(multiSelect)
                .rememberSelected(rememberSelected)
                .btnBgColor(Color.TRANSPARENT)
                .btnTextColor(Color.WHITE)
                .statusBarColor(context.getResources().getColor(R.color.theme_color_primary))
                .backResId(R.drawable.ic_back)
                .title("图片选择")
                .titleColor(Color.WHITE)
                .titleBgColor(context.getResources().getColor(R.color.theme_color_primary))
                .needCrop(needCrop)
                .cropSize(1, 1, 200, 200)
                .needCamera(needCamera)
                .maxNum(maxCount)
                .build();
        return config;
    }

    /**
     * 图片显示监听
     */
    private static class OnImageLoader implements ImageLoader {
        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            Glide.with(context)
                    .load(path)
                    .into(imageView);
        }
    }
}